package com.night.xjh.controller;

import com.github.pagehelper.PageInfo;
import com.night.common.entity.ResultVo;
import com.night.common.entity.xjh.Callback;
import com.night.xjh.vo.CallbackVo;
import com.night.xjh.service.CallbackService;
import com.night.xjh.service.CallbackTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: CallbackController
 * @Description: //TODO
 * @author: xjh
 * @date: 2022/10/24-9:12
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/xjh/callback")
public class CallbackController {
    @Autowired
    private CallbackService callbackService;
    @Autowired
    private CallbackTypeService callbackTypeService;
    @RequestMapping("/add")
    public ResultVo<?> add(@RequestBody CallbackVo callbackVo){
        callbackService.add(dataFormat(callbackVo));
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/delete")
    public ResultVo<?> delete(@RequestBody CallbackVo callbackVo){
        callbackService.delete(callbackVo.getId());
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/update")
    public ResultVo<?> update(@RequestBody CallbackVo callbackVo){
        callbackService.update(dataFormat(callbackVo));
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/getList")
    public ResultVo<?> getList(@RequestBody CallbackVo callbackVo){
        if((callbackVo.getContent()!=null&&!callbackVo.getContent().equals(""))
                ||(callbackVo.getCallbackTypeId()!=null&&!callbackVo.getCallbackTypeId().equals(""))){
            return new ResultVo<>(callbackService.getByCondition(dataFormat(callbackVo),callbackVo.getPageNum(),callbackVo.getPageSize()));
        }else{
            return new ResultVo<>(callbackService.getAll(callbackVo.getPageNum(),callbackVo.getPageSize()));
        }
    }
    //    @RequestMapping("/getAll")
//    public ResultVo<?> getAll(@RequestBody CallbackVo callbackVo){
//        PageInfo<Callback> pageInfo=callbackService.getAll(callbackVo.getPageNum(),callbackVo.getPageSize());
//        return new ResultVo<>(pageInfo);
//    }
    @RequestMapping("/getById")
    public ResultVo<?> getById(@RequestBody CallbackVo callbackVo){
        return new ResultVo<>(callbackService.getById(callbackVo.getId()));
    }
    //    @RequestMapping("/getByCondition")
//    public ResultVo<?> getByCondition(@RequestBody CallbackVo callbackVo){
//        PageInfo<Callback> pageInfo=callbackService.getByCondition
//                (dateFormat(callbackVo),
//                callbackVo.getPageNum(),
//                callbackVo.getPageSize());
//        return new ResultVo<>(pageInfo);
//    }
    private Callback dataFormat(CallbackVo callbackVo){
        Callback callback = new Callback();
        callback.setId(callbackVo.getId());
        callback.setContent(callbackVo.getContent());
        callback.setCallbackType(callbackTypeService.getById(callbackVo.getCallbackTypeId()));
        return callback;
    }
    //返回
    @RequestMapping("/getAllType")
    public ResultVo<?> getAllType(){
        return new ResultVo<>(callbackTypeService.getAll());
    }
    @RequestMapping("/getTypeById")
    public ResultVo<?> getTypeById(@RequestBody CallbackVo callbackVo){
        return new ResultVo<>(callbackTypeService.getById(callbackVo.getCallbackTypeId()));
    }
    @RequestMapping("/getTypeByCondition")
    public ResultVo<?> getTypeByCondition(@RequestBody CallbackVo callbackVo){
        return new ResultVo<>(callbackTypeService.getByName(
                callbackVo.getTypeName()
        ));
    }

}
